﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;

namespace LocationProvider
{
    public partial class Step2 : PortalModuleBase
    {
        public IEnumerable<cwc_Provider> SearchResults { get; set; }
        private CWCareProviderDataContext _context = new CWCareProviderDataContext();

        private IEnumerable<cwc_Provider> Search(SearchCriteria searchCriteria)
        {
            var query = _context.cwc_Providers.AsQueryable();

            if (!string.IsNullOrEmpty(searchCriteria.ProviderID) && searchCriteria.ProviderID != "Any")
            {
                int proID = int.Parse(searchCriteria.ProviderID);
                query = query.Where(p => p.providerID == proID);
            }
            
            if (!string.IsNullOrEmpty(searchCriteria.City) && searchCriteria.City != "Any")
            {
                query = from q in query
                        join l in _context.cwc_Location_Providers on q.providerID equals l.providerID
                        join lp in _context.cwc_Locations on l.addressID equals lp.addressID
                        where lp.city == searchCriteria.City
                        select q ;
            }

            if (!string.IsNullOrEmpty(searchCriteria.County) && searchCriteria.County != "Any")
            {
                query = from q in query
                        join l in _context.cwc_Location_Providers on q.providerID equals l.providerID
                        join lp in _context.cwc_Locations on l.addressID equals lp.addressID
                        where lp.county == searchCriteria.County
                        select q;
            }

            if (!string.IsNullOrEmpty(searchCriteria.Specialty) && searchCriteria.Specialty != "Any")
            {
                int specialtyID = int.Parse(searchCriteria.Specialty);
                query = from q in query
                        join sp in _context.cwc_Specialty_Providers on q.providerID equals sp.providerID
                        join s in _context.cwc_Specialties on sp.specialtyID equals s.specialtyID
                        where s.specialtyID == specialtyID
                        select q;
            }

            if (!string.IsNullOrEmpty(searchCriteria.Language) && searchCriteria.Language != "Any")
            {
                int languageID = int.Parse(searchCriteria.Language);
                query = from q in query
                        join sp in _context.cwc_SpokenLanguage_Providers on q.providerID equals sp.providerID
                        join l in _context.cwc_SpokenLanguages on sp.languageID equals l.languageID
                        where l.languageID == languageID
                        select q;
            }

            if (!string.IsNullOrEmpty(searchCriteria.Hospital) && searchCriteria.Hospital != "Any")
            {
                int hospitalID = int.Parse(searchCriteria.Hospital);
                query = from q in query
                        join hp in _context.cwc_Hospital_Providers on q.providerID equals hp.providerID
                        join h in _context.cwc_Hospitals on hp.hospitalID equals h.hospitalID
                        where h.hospitalID == hospitalID
                        select q;
            }

            return query.OrderBy(p=>p.lName);
        }


        protected void Page_Load(object sender, EventArgs e)
        {
			SearchCriteria sc = new SearchCriteria();
            if (Session["CWCSearchCriteria"] != null)
            {
                sc = (SearchCriteria)Session["CWCSearchCriteria"];
			}
			ProviderSearchResults.DataSource = this.Search(sc);
			ProviderSearchResults.DataBind();

			this.SearchResults = this.Search(sc);
        }

        protected void ProviderSearchResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Repeater ProviderLocations = e.Item.FindControl("ProviderLocations") as Repeater;

            List<cwc_Location> listLocation = new List<cwc_Location>();
            cwc_Provider provider = e.Item.DataItem as cwc_Provider;
            foreach(cwc_Location_Provider lp in provider.cwc_Location_Providers)
            {
                cwc_Location loc = _context.cwc_Locations.SingleOrDefault(p => p.addressID == lp.addressID);
                if (loc != null)
                {
                    listLocation.Add(loc);
                }
            }
            ProviderLocations.DataSource = listLocation;
            ProviderLocations.DataBind();
        }
    }

    public partial class cwc_Provider
    {
        public string DisplaySpecialty
        {
            get
            {
                string specialty = string.Empty;
                foreach (cwc_Specialty_Provider sp in this.cwc_Specialty_Providers)
                {
                    specialty +=  (!string.IsNullOrEmpty(specialty) ? ", " : "") + sp.cwc_Specialty.specialty;
                }

                return specialty;
            }
        }

        public string DisplayFullName
        {
            get
            {
                cwc_Certification_Provider cp = this.cwc_Certification_Providers.FirstOrDefault();
                string certification = cp != null ? cp.cwc_Certification.certification : string.Empty;
                return fName + " " + lName + ", " + certification;
            }
        }

        public string ProviderDetailURL
        {
            get
            {
                return "Provider.aspx?pid=" + this.providerID.ToString();
            }
        }
    }
}